package org.edu.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.edu.dao.DeptDao;
import org.edu.dao.TreeNodeDao;
import org.edu.model.Dept;
import org.edu.model.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = { RuntimeException.class }, propagation = Propagation.REQUIRED)
public class TreeNodeService extends ServiceAdapter<TreeNode, Integer> {
	
	private static final Logger logger = Logger.getLogger(TreeNodeService.class);
	@Autowired
	private TreeNodeDao treeNodeDao;
	@Autowired
	private DeptDao deptdao;



//	public List<TreeNode> getNode(int pid) {
//		List<TreeNode> list = treeNodeDao
//				.find("from TreeNode where pid=" + pid);
//		if (list != null && list.size() != 0) {// 判断是否查到,没有了表示小的了
//			for (int i = 0; i < list.size(); i++) {
//				// System.out.print(list.get(i).getId());
//				list.get(i).setChildren(getNode(list.get(i).getId())); // 根据当前id查询子
//			}
//		}
//		return list;
//	}

	public List<TreeNode> list(String id) {
		if(null!=id){
			idlist = new ArrayList<TreeNode>();
			idlist.addAll(idNode(id));
			return idlist;
		}
		return list();
	}
	private List<TreeNode> idlist = new ArrayList<TreeNode>();
	/**
	 * 递归构成树
	 * @param id
	 * @return
	 */
	public List<TreeNode> idNode(String id){
		List<TreeNode> list=treeNodeDao.find("from TreeNode where pid=" + id);
		if (list != null && list.size() != 0) {// 判断是否查到,没有了表示小的了
			for(int i = 0; i < list.size(); i++){
				idlist.addAll(idNode(list.get(i).getId()+""));
			}
		}
		
		return list;
	}
	@Override
	public List<TreeNode> list() {
		// TODO Auto-generated method stub
		return treeNodeDao.loadAll();
	}

	@Override
	public void update(TreeNode object) {
		// TODO Auto-generated method stub
		//修改树的同时修改实体
		Dept de=deptdao.get(object.getId());
		de.setDeptname(object.getName());
		deptdao.update(de);
		treeNodeDao.update(object);
	}

	@Override
	public void save(TreeNode object) {
		// TODO Auto-generated method stub
		treeNodeDao.save(object);
	}

	@Override
	public void delete(Integer id) {
		// TODO Auto-generated method stub
		logger.info("删除id："+id+"已删除："+cont);
		deptdao.delete(deptdao.get(id));
	}

	@Override
	public TreeNode show(Integer id) {
		// TODO Auto-generated method stub
		return treeNodeDao.get(id);
	}
	/**
	 * 递归删除一个科室下所有的部门
	 */
	private int cont=0;
	public void delete(Integer id,boolean isparent){
		
		this.delete(id);
		cont++;
		if(isparent){
			List<TreeNode> list = treeNodeDao
					.find("from TreeNode where pid=" + id);
			if (list != null && list.size() != 0) {// 判断是否查到,没有了表示小的了
				for (int i = 0; i < list.size(); i++) {
					// System.out.print(list.get(i).getId());
					this.delete(list.get(i).getId(),!(treeNodeDao.find("from TreeNode where pid=" + list.get(i).getId()).isEmpty()));
					//list.get(i).setChildren(getNode(list.get(i).getId())); // 根据当前id查询子
				}
			}
		}
	}

	public int getCont() {
		return cont;
	}

	public void setCont(int cont) {
		this.cont = cont;
	}
	
}
